| 1 | import { Stack, router, useFocusEffect, useLocalSearchParams } from "expo-router"; |
| 2 | import * as WebBrowser from "expo-web-browser"; |
| 3 | import { ExternalLink, FileCode, GitPullRequest, ListTodo, MessageSquareText, Sparkles, StickyNote, Terminal, User, Wrench } from "lucide-react-native"; |
| 4 | import type { LucideIcon } from "lucide-react-native"; |
| 5 | import { useCallback, useState } from "react"; |
| 6 | import { Pressable, RefreshControl, ScrollView, StyleSheet, View } from "react-native"; |
| 7 | |
| 8 | import { StateDot, openItem, openPull, stateName, useCode } from "@/components/sessions"; |
| 9 | import { Badge, Button, ErrorView, Loading, Row, Screen, SectionHeader, Text } from "@/components/ui"; |
| 10 | import { Chip, repoName } from "@/components/work"; |
| 11 | import { siteUrl } from "@/lib/server"; |
| 12 | import { useSignedIn } from "@/lib/session"; |
| 13 | import { type ApiSessionEntry, type ApiSessionLink, type EntryKind, harnessName, sessionTitle, subagentTree, touchedFiles } from "@/lib/sessions"; |
| 14 | import { ago, clock } from "@/lib/time"; |
| 15 | import { useWorkspaceSession } from "@/lib/work"; |
| 16 | import { radius, space, useColors } from "@/theme"; |
| 17 | |
| 18 | /** How many files show before "Show all". */ |
| 19 | const FILES = 20; |
| 20 | |
| 21 | /** |
| 22 | * One session: who runs it in what, where, and its state; the work item |
| 23 | * and pull request it is on; its subagents as a tree; the files it |
| 24 | * touched; and what it recorded, step by step. While it is live it is |
| 25 | * read again every 15 s. |
| 26 | */ |
| 27 | export default function SessionScreen() { |
| 28 | const params = useLocalSearchParams<{ id: string; workspace?: string }>(); |
| 29 | const { server, workspace: current } = useSignedIn(); |
| 30 | const workspace = params.workspace ?? current?.slug ?? ""; |
| 31 | const colors = useColors(); |
| 32 | const code = useCode(); |
| 33 | const [focused, setFocused] = useState(false); |
| 34 | useFocusEffect( |
| 35 | useCallback(() => { |
| 36 | setFocused(true); |
| 37 | return () => setFocused(false); |
| 38 | }, []), |
| 39 | ); |
| 40 | const query = useWorkspaceSession(workspace, params.id, focused); |
| 41 | const [allFiles, setAllFiles] = useState(false); |
| 42 | const sitePath = `/${workspace}/-/work/sessions/${params.id}`; |
| 43 | |
| 44 | const title = ( |
| 45 | <Stack.Screen |
| 46 | options={{ |
| 47 | title: "Session", |
| 48 | headerRight: () => ( |
| 49 | <Pressable accessibilityLabel="Open on g1t" hitSlop={10} onPress={() => void WebBrowser.openBrowserAsync(siteUrl(server, sitePath))}> |
| 50 | <ExternalLink size={20} color={colors.fg} /> |
| 51 | </Pressable> |
| 52 | ), |
| 53 | }} |
| 54 | /> |
| 55 | ); |
| 56 | if (query.isPending || query.error || !query.detail) { |
| 57 | return ( |
| 58 | <Screen> |
| 59 | {title} |
| 60 | {query.error ? <ErrorView error={query.error} retry={() => query.refetch()} /> : <Loading />} |
| 61 | </Screen> |
| 62 | ); |
| 63 | } |
| 64 | |
| 65 | const { session, repo, children = [], touched, skills = [], root, limited } = query.detail; |
| 66 | const links: ApiSessionLink[] = query.detail.links?.length |
| 67 | ? query.detail.links |
| 68 | : [ |
| 69 | ...(session.item ? [{ kind: "item" as const, number: session.item, title: "", state: "", via: ["attached"], session_ids: [session.id] }] : []), |
| 70 | ...(session.pull ? [{ kind: "pull" as const, number: session.pull, title: "", state: "", via: ["attached"], session_ids: [session.id] }] : []), |
| 71 | ]; |
| 72 | const tree = subagentTree(session.id, children); |
| 73 | const files = touchedFiles(touched); |
| 74 | const facts = [harnessName(session.harness), `@${session.actor.username}`, session.model].filter(Boolean).join(" · "); |
| 75 | const openSession = (id: string) => router.push({ pathname: "/work/sessions/[id]", params: { id, workspace } }); |
| 76 | |
| 77 | return ( |
| 78 | <Screen> |
| 79 | {title} |
| 80 | <ScrollView |
| 81 | contentContainerStyle={styles.content} |
| 82 | refreshControl={<RefreshControl refreshing={query.isRefetching && !query.isFetchingNextPage} onRefresh={() => query.refetch()} tintColor={colors.accent} />} |
| 83 | > |
| 84 | <View style={{ gap: space.sm }}> |
| 85 | <Text size="heading" selectable> |
| 86 | {sessionTitle(session)} |
| 87 | </Text> |
| 88 | <View style={styles.wrap}> |
| 89 | <StateDot state={session.state} /> |
| 90 | <Text size="small" style={{ fontWeight: "600", color: session.state === "active" ? colors.success : session.state === "idle" ? colors.warn : colors.muted }}> |
| 91 | {stateName(session.state)} |
| 92 | </Text> |
| 93 | <Text size="small" tone="muted"> |
| 94 | {facts} |
| 95 | </Text> |
| 96 | </View> |
| 97 | <Text size="small" tone="muted"> |
| 98 | {repoName(repo, workspace)} |
| 99 | {session.branch ? ` · ${session.branch}` : ""} |
| 100 | </Text> |
| 101 | <Text size="small" tone="muted"> |
| 102 | Started {ago(session.started_at)} · last heard from {ago(session.last_seen_at)} |
| 103 | {session.ended_at ? ` · ended ${ago(session.ended_at)}${session.end_reason ? ` (${session.end_reason.replace(/_/g, " ")})` : ""}` : ""} |
| 104 | </Text> |
| 105 | {session.agent_type || session.task ? ( |
| 106 | <Text size="small" tone="muted"> |
| 107 | {[session.agent_type, session.task].filter(Boolean).join(": ")} |
| 108 | </Text> |
| 109 | ) : null} |
| 110 | {root && root.id !== session.id ? ( |
| 111 | <View style={styles.wrap}> |
| 112 | <Text size="small" tone="muted"> |
| 113 | A subagent of |
| 114 | </Text> |
| 115 | <Chip label={sessionTitle(root)} onPress={() => openSession(root.id)} /> |
| 116 | </View> |
| 117 | ) : null} |
| 118 | {skills.length ? ( |
| 119 | <View style={styles.wrap}> |
| 120 | {skills.map((skill) => ( |
| 121 | <Chip key={`${skill.session_id}:${skill.name}`} label={skill.uses > 1 ? `${skill.name} ×${skill.uses}` : skill.name} leading={<Sparkles size={12} color={colors.muted} />} /> |
| 122 | ))} |
| 123 | </View> |
| 124 | ) : null} |
| 125 | {limited ? ( |
| 126 | <Text size="small" tone="faint"> |
| 127 | You are not a member of this workspace: you see that it runs, who runs it and what it is tied to, nothing more. |
| 128 | </Text> |
| 129 | ) : null} |
| 130 | </View> |
| 131 | |
| 132 | {links.length ? ( |
| 133 | <View style={{ marginHorizontal: -space.lg }}> |
| 134 | <SectionHeader title="Working on" /> |
| 135 | {links.map((link) => ( |
| 136 | <Row |
| 137 | key={`${link.kind}:${link.number}`} |
| 138 | leading={link.kind === "item" ? <ListTodo size={20} color={colors.muted} /> : <GitPullRequest size={20} color={colors.muted} />} |
| 139 | title={`#${link.number}${link.title ? ` ${link.title}` : ""}`} |
| 140 | subtitle={[link.kind === "item" ? "Work item" : "Pull request", link.state, link.via.join(", ")].filter(Boolean).join(" · ")} |
| 141 | onPress={() => (link.kind === "item" ? openItem(workspace, link.number) : openPull(repo, link.number, code))} |
| 142 | /> |
| 143 | ))} |
| 144 | </View> |
| 145 | ) : null} |
| 146 | |
| 147 | {tree.length ? ( |
| 148 | <View style={{ marginHorizontal: -space.lg }}> |
| 149 | <SectionHeader title={`Subagents · ${tree.length}`} /> |
| 150 | {tree.map(({ session: child, depth }) => ( |
| 151 | <View key={child.id} style={{ paddingLeft: (depth - 1) * space.lg }}> |
| 152 | <Row |
| 153 | leading={<StateDot state={child.state} />} |
| 154 | title={sessionTitle(child)} |
| 155 | subtitle={[child.agent_type, child.state === "ended" ? `ended ${ago(child.ended_at ?? child.last_seen_at)}` : `heard from ${ago(child.last_seen_at)}`].filter(Boolean).join(" · ")} |
| 156 | onPress={() => openSession(child.id)} |
| 157 | /> |
| 158 | </View> |
| 159 | ))} |
| 160 | </View> |
| 161 | ) : null} |
| 162 | |
| 163 | {files.length ? ( |
| 164 | <View style={{ gap: space.xs }}> |
| 165 | <View style={{ marginHorizontal: -space.lg }}> |
| 166 | <SectionHeader title={`Touched · ${files.length}`} /> |
| 167 | </View> |
| 168 | {(allFiles ? files : files.slice(0, FILES)).map((path) => ( |
| 169 | <View key={path} style={styles.file}> |
| 170 | <FileCode size={14} color={colors.faint} /> |
| 171 | <Text size="mono" tone="muted" numberOfLines={1} style={{ flex: 1 }}> |
| 172 | {path} |
| 173 | </Text> |
| 174 | </View> |
| 175 | ))} |
| 176 | {files.length > FILES && !allFiles ? ( |
| 177 | <Pressable onPress={() => setAllFiles(true)} hitSlop={8}> |
| 178 | <Text size="small" tone="accent"> |
| 179 | Show all {files.length} |
| 180 | </Text> |
| 181 | </Pressable> |
| 182 | ) : null} |
| 183 | </View> |
| 184 | ) : null} |
| 185 | |
| 186 | <View style={{ gap: space.sm }}> |
| 187 | <View style={{ marginHorizontal: -space.lg }}> |
| 188 | <SectionHeader title="Timeline" /> |
| 189 | </View> |
| 190 | {query.entries.length === 0 ? ( |
| 191 | <Text tone="muted">{limited ? "Its steps are not shown to you." : "Nothing recorded yet."}</Text> |
| 192 | ) : ( |
| 193 | query.entries.map((entry) => <Entry key={entry.seq} entry={entry} />) |
| 194 | )} |
| 195 | {query.hasNextPage ? ( |
| 196 | <Button title="Show later steps" variant="secondary" busy={query.isFetchingNextPage} onPress={() => void query.fetchNextPage()} /> |
| 197 | ) : null} |
| 198 | </View> |
| 199 | </ScrollView> |
| 200 | </Screen> |
| 201 | ); |
| 202 | } |
| 203 | |
| 204 | const ENTRY_KINDS: Record<EntryKind, { label: string; icon: LucideIcon }> = { |
| 205 | prompt: { label: "Prompt", icon: User }, |
| 206 | message: { label: "Message", icon: MessageSquareText }, |
| 207 | tool_call: { label: "Tool", icon: Wrench }, |
| 208 | tool_result: { label: "Result", icon: Terminal }, |
| 209 | note: { label: "Note", icon: StickyNote }, |
| 210 | skill: { label: "Skill", icon: Sparkles }, |
| 211 | }; |
| 212 | |
| 213 | /** One recorded step: its kind, tool and time, then its text, a few lines until tapped. */ |
| 214 | function Entry({ entry }: { entry: ApiSessionEntry }) { |
| 215 | const colors = useColors(); |
| 216 | const [open, setOpen] = useState(false); |
| 217 | const kind = ENTRY_KINDS[entry.kind] ?? { label: entry.kind, icon: StickyNote }; |
| 218 | const Icon = kind.icon; |
| 219 | const result = entry.kind === "tool_result" || entry.kind === "tool_call"; |
| 220 | return ( |
| 221 | <Pressable onPress={() => setOpen(!open)} style={[styles.entry, { borderColor: colors.line, backgroundColor: entry.kind === "prompt" ? colors.raised : colors.surface }]}> |
| 222 | <View style={styles.wrap}> |
| 223 | <Icon size={13} color={entry.kind === "prompt" ? colors.accent : colors.muted} /> |
| 224 | <Text size="small" style={{ fontWeight: "600", color: colors.fgSoft }}> |
| 225 | {kind.label} |
| 226 | </Text> |
| 227 | {entry.tool ? <Badge label={entry.tool} /> : null} |
| 228 | <Text size="small" tone="faint" style={{ marginLeft: "auto" }}> |
| 229 | {clock(entry.at)} |
| 230 | </Text> |
| 231 | </View> |
| 232 | {entry.withheld ? ( |
| 233 | <Text size="small" tone="faint" style={{ fontStyle: "italic" }}> |
| 234 | Not shown to you. |
| 235 | </Text> |
| 236 | ) : entry.text ? ( |
| 237 | <Text size={result ? "mono" : "small"} tone={result ? "muted" : "default"} numberOfLines={open ? undefined : 4} selectable={open}> |
| 238 | {entry.text} |
| 239 | </Text> |
| 240 | ) : null} |
| 241 | </Pressable> |
| 242 | ); |
| 243 | } |
| 244 | |
| 245 | const styles = StyleSheet.create({ |
| 246 | content: { padding: space.lg, gap: space.lg, paddingBottom: space.xl }, |
| 247 | wrap: { flexDirection: "row", flexWrap: "wrap", alignItems: "center", gap: 6 }, |
| 248 | file: { flexDirection: "row", alignItems: "center", gap: space.sm }, |
| 249 | entry: { borderWidth: 1, borderRadius: radius.md, padding: space.md, gap: space.xs }, |
| 250 | }); |